<?php
namespace app\index\controller;


class  Upload
{
	 public function  file()
	 {
	 
$typeArr = array("doc","docx","jpg","mp3","png","mp4","pdf", "gif", "jpeg", "mov", "gears", "html5", "html4", "silverlight", "flash"); //允许上传文件格式
//分时间文件夹
$time=date('Ymd',time());
$path = "uploads/2019070809/"; //上传路径

if (isset($_POST)) {
    $name = $_FILES['file']['name'];
    $size = $_FILES['file']['size'];
    $name_tmp = $_FILES['file']['tmp_name'];
    if (empty($name)) {
        echo json_encode(array("error" => "您还未选择文件"));
        exit;
    }
//    print_r($_FILES['file']);
    $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型
    if (!in_array($type, $typeArr)) {
        echo json_encode(array("error" => "清上传指定类型的文件！","type"=>"types"));
        exit;
    }
/*
    if ($size >= (100 * 1024)) { //上传大小
        echo json_encode(array("error" => "文件大小已超过100MB！","type"=>"size"));
        exit;
    }
*/
    if (in_array($type,array("jpg,png,jpeg,JPG,PNG,JPEG"))&&$size > (20 * 1024)) { //上传大小
        echo json_encode(array("error" => "单张≤20MB，不超过5张；","type"=>"size"));
        exit;
    }
    if (in_array($type,array("pdf,PDF"))&&$size > (50 * 1024)) { //上传大小
        echo json_encode(array("error" => "DF≤50MB；","type"=>"size"));
        exit;
    }
    if (in_array($type,array("mp4,MP4"))&&$size > (300 * 1024)) { //上传大小
        echo json_encode(array("error" => "MP4≤300MB，要求为H.264编码；","type"=>"size"));
        exit;
    }

    $pic_name = time() . rand(10000, 99999) . "." . $type; //文件名称
    $pic_url = $path . $pic_name; //上传后图片路径+名称
    if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹
        echo json_encode(array("error" => "恭喜你上传成功", "pic" => $pic_url, "name" => $name));
    } 
    else {
        echo json_encode(array("error" => "上传有误，清检查服务器配置！","type"=>"config"));
    }
}

	 }







}



?>